package exam.controller;

import com.alibaba.fastjson.JSONObject;
import exam.config.ThreadPoolService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author Xiongx
 * @version 1.0
 * @date 2021/4/3 16:34
 * @since JDK 1.8
 */
@RestController
@Api(tags = {"线程测试模块"})
@Slf4j
public class ThreadController {


    @Autowired
    private ThreadPoolTaskExecutor asyncServiceExecutor;

    @GetMapping("ThreadTest1")
    @ApiOperation("线程测试模块")
    public JSONObject testThreadController(){
        JSONObject jsonObject=new JSONObject();
        asyncServiceExecutor.execute(new Runnable() {
            @Override
            public void run() {
                Long start=System.currentTimeMillis();
                for (int i = 0; i < 10000000 ; i++) {
                    System.out.println(i);
                }
                Long end=System.currentTimeMillis();
                log.info("执行需要"+((end-start)/1000));
            }
        });
        jsonObject.put("code",0);
        jsonObject.put("msg","success");
        return jsonObject;
    }

    @GetMapping("ThreadTest2")
    @ApiOperation("线程测试模块2")
    public JSONObject testThreadController2(){
        JSONObject jsonObject=new JSONObject();

        ThreadPoolService.getInstance().execute(new Runnable() {
            @Override
            public void run() {
                Long start=System.currentTimeMillis();
                for (int i = 0; i < 10000000 ; i++) {
                    System.out.println(i);
                }
                Long end=System.currentTimeMillis();
                log.info("执行需要"+((end-start)/1000));
            }
        });
        jsonObject.put("code",0);
        jsonObject.put("msg","success");
        return jsonObject;
    }
}
